<template>
  <div class="article-list">
    <van-pull-refresh
      v-model="isFreshLoading"
      :success-text="reFreshText"
      @refresh="onRefresh"
      :success-duration="1000"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
      >
        <article-item
          v-for="(article, index) in list"
          :key="index"
          :article="article"
        />
        <!-- <van-cell
          v-for="(article, index) in list"
          :key="index"
          :title="article.title"
        /> -->
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticles } from '@/api/article'
import ArticleItem from '@/components/article-item'

export default {
  name: 'ArticleList',
  components: {
    ArticleItem,
  },
  props: {
    channel: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      // 存储列表数据得数组
      list: [],
      // 控制加载中loading的状态
      loading: false,
      // 控制加载结束的状态
      finished: false,
      // 请求获取下一页数据的时间戳
      timestamp: null,
      // 控制列表加载失败的提示状态
      error: false,
      // 控制下拉刷新的loading状态
      isFreshLoading: false,
      // 更新下拉的文本提示框
      reFreshText: '',
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 初始化 或滚动到底部的时候会触发这个函数
    async onLoad() {
      try {
        // 1.请求获取数据
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: this.timestamp || Date.now(),
          with_top: 0,
        })
        // console.log(data)

        // 模拟加载失败
        // if (Math.random() > 0.5) {
        //   JSON.parse('1123asdasdas')
        // }

        // 2.把请求结果放到list数组当中
        const { results } = data.data
        this.list.push(...results)
        // 3.本次数据加载结束之后要把加载状态设置为结束
        this.loading = false
        // 4.判断数据是否全部加载完毕
        if (results.length) {
          // 更新获取下一页得时间戳
          this.timestamp = data.data.pre_timestamp
        } else {
          // 没有数据了
          this.finished = true
        }
      } catch (err) {
        // console.log(err)
        // 展示错误提示状态
        this.error = true
        // 请求失败了 ，loading也要关闭
        this.loading = false
      }
    },
    // 下拉刷新的时候会触发这个函数
    async onRefresh() {
      try {
        // 请求获取数据
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: Date.now(),
          with_top: 1,
        })
        // 将数据追加到list列表的顶部
        const { results } = data.data
        this.list.unshift(...results)
        // 关闭loading的状态
        this.isFreshLoading = false
        // 更新下拉刷新成功的文本提示
        this.reFreshText = `刷新成功，更新了${results.length}条数据`
      } catch (err) {
        this.isFreshLoading = false
        this.reFreshText = '刷新失败'
      }
    },
  },
}
</script>
<style lang='less' scoped>
.article-list {
  height: 79vh;
  overflow-y: auto;
}
</style>
